<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猫咖管理系统 - 订单管理</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 全局样式 */
        :root {
            --primary-color: #D85D5D;
            --secondary-color: #9B7E46;
            --accent-color: #FFA384;
            --light-color: #F8F3EA;
            --dark-color: #333333;
            --success-color: #4CAF50;
            --error-color: #FF5252;
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f9f9f9;
            color: var(--dark-color);
            line-height: 1.6;
        }

        .container {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
        }

        /* 顶部导航栏样式 */
        header {
            background-color: white;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .header-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 0;
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .logo-icon {
            width: 40px;
            height: 40px;
            background-color: var(--primary-color);
            border-radius: 50%;
            position: relative;
        }

        .logo-icon::after {
            content: '';
            position: absolute;
            top: -4px;
            left: -4px;
            width: 48px;
            height: 48px;
            border-radius: 50%;
            border: 1px solid rgba(216, 93, 93, 0.2);
        }

        .logo h1 {
            font-size: 24px;
            color: var(--primary-color);
        }

        nav ul {
            display: flex;
            list-style: none;
            gap: 25px;
        }

        nav a {
            text-decoration: none;
            color: var(--dark-color);
            font-weight: 500;
            padding: 8px 12px;
            border-radius: 4px;
            transition: var(--transition);
            position: relative;
        }

        nav a:hover,
        nav a.active {
            color: var(--primary-color);
        }

        nav a::after {
            content: '';
            position: absolute;
            bottom: -5px;
            left: 0;
            width: 0;
            height: 2px;
            background-color: var(--primary-color);
            transition: var(--transition);
        }

        nav a:hover::after,
        nav a.active::after {
            width: 100%;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-info span {
            font-weight: 500;
        }

        .btn {
            padding: 8px 16px;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            font-weight: 500;
            transition: var(--transition);
        }

        .btn-primary {
            background-color: var(--primary-color);
            color: white;
        }

        .btn-primary:hover {
            background-color: #c74d4d;
        }

        /* 订单内容区域 */
        .orders-board {
            padding: 60px 0;
            background-color: #fff;
            min-height: calc(100vh - 200px);
        }

        .section-title {
            text-align: center;
            margin-bottom: 40px;
            position: relative;
        }

        .section-title h2 {
            font-size: 32px;
            color: var(--primary-color);
            display: inline-block;
            margin-bottom: 15px;
        }

        .section-title::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 80px;
            height: 3px;
            background-color: var(--secondary-color);
        }

        /* 订单列表样式 */
        .orders-scroll-container {
            max-height: 65vh;
            overflow-y: auto;
            padding-right: 10px;
            margin-top: 20px;
        }

        .orders-scroll-container::-webkit-scrollbar {
            width: 8px;
        }
        .orders-scroll-container::-webkit-scrollbar-thumb {
            background-color: rgba(0, 0, 0, 0.2);
            border-radius: 4px;
        }
        .orders-scroll-container::-webkit-scrollbar-track {
            background-color: transparent;
        }

        .orders-container {
            display: flex;
            flex-direction: column;
            gap: 25px;
        }

        .order-card {
            background-color: white;
            border-radius: 12px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            border-left: 4px solid var(--accent-color);
            transition: var(--transition);
            position: relative;
        }

        .order-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
        }

        .order-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }

        .order-info {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .order-id {
            font-weight: 600;
            font-size: 18px;
            color: var(--primary-color);
        }

        .order-time {
            color: #95a5a6;
            font-size: 14px;
        }

        .order-details {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }

        .detail-item {
            display: flex;
            flex-direction: column;
        }

        .detail-label {
            font-size: 14px;
            color: #7f8c8d;
            margin-bottom: 5px;
        }

        .detail-value {
            font-size: 16px;
            font-weight: 500;
        }

        .total-price {
            font-size: 18px;
            font-weight: 600;
            color: var(--primary-color);
        }

        .order-actions {
            display: flex;
            justify-content: flex-end;
            gap: 15px;
            margin-top: 20px;
        }

        .delete-btn {
            background-color: var(--error-color);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
            gap: 8px;
            font-weight: 500;
        }

        .delete-btn:hover {
            background-color: #e04141;
            transform: translateY(-2px);
            box-shadow: 0 3px 8px rgba(255, 82, 82, 0.3);
        }

        .empty-state {
            text-align: center;
            padding: 50px 20px;
            background-color: white;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
        }

        .empty-state i {
            font-size: 48px;
            color: var(--accent-color);
            margin-bottom: 20px;
        }

        .empty-state h3 {
            font-size: 24px;
            color: var(--primary-color);
            margin-bottom: 15px;
        }

        .empty-state p {
            color: #666;
            max-width: 500px;
            margin: 0 auto;
        }

        /* 页脚样式 */
        footer {
            background-color: var(--dark-color);
            color: white;
            padding: 40px 0 20px;
        }

        .footer-content {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 30px;
            margin-bottom: 30px;
        }

        .footer-column h3 {
            font-size: 20px;
            margin-bottom: 20px;
            position: relative;
            padding-bottom: 10px;
        }

        .footer-column h3::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 50px;
            height: 2px;
            background-color: var(--primary-color);
        }

        .footer-links {
            list-style: none;
        }

        .footer-links li {
            margin-bottom: 10px;
        }

        .footer-links a {
            color: #bbb;
            text-decoration: none;
            transition: var(--transition);
        }

        .footer-links a:hover {
            color: white;
            padding-left: 5px;
        }

        .contact-info {
            color: #bbb;
        }

        .contact-info p {
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .copyright {
            text-align: center;
            padding-top: 20px;
            border-top: 1px solid #444;
            color: #bbb;
            font-size: 14px;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .header-container {
                flex-direction: column;
                gap: 15px;
            }

            nav ul {
                flex-wrap: wrap;
                justify-content: center;
                gap: 10px;
            }

            .order-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }

            .order-actions {
                justify-content: center;
            }

            .order-details {
                grid-template-columns: 1fr;
            }
        }

        .coffee-name {
            font-size: 18px;
            font-weight: 600;
            color: var(--secondary-color);
            margin: 10px 0 15px;
            padding-bottom: 10px;
            border-bottom: 1px dashed #eee;
        }
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<header>
    <div class="container header-container">
        <div class="logo">
            <div class="logo-icon"></div>
            <h1>猫咖管理系统</h1>
        </div>

        <nav>
            <ul>
                <li><a href="index.html">首页</a></li>
                <li><a href="cat.html">猫咪预约</a></li>
                <li><a href="cafe.html">咖啡购买</a></li>
                <li><a href="center.html">个人中心</a></li>
                <li><a href="message.html">留言板</a></li>
<!--                <li><a href="shoppingcart.html">购物车</a></li>-->
                <li><a href="orders.html" class="active">订单信息</a></li>
                <li><a href="booking.html">预约信息</a></li>
            </ul>
        </nav>

        <!-- 顶部导航栏的用户信息部分 -->
        <div class="user-info">
            <span id="user-display">未登录</span>
            <button class="btn btn-primary" id="logout-btn">退出</button>
        </div>
    </div>
</header>

<!-- 订单内容区域 -->
<section class="orders-board">
    <div class="container">
        <div class="section-title">
            <h2>订单管理</h2>
            <p>查看和管理所有用户订单信息</p>
        </div>

        <!-- 订单列表 -->
        <div class="orders-scroll-container">
            <div class="orders-container" id="ordersContainer">
                <!-- 订单卡片会动态加载到这里 -->
            </div>
        </div>
    </div>
</section>

<!-- 页脚 -->
<footer>
    <div class="container">
        <div class="footer-content">
            <div class="footer-column">
                <h3>关于我们</h3>
                <p>猫咖管理系统致力于为猫咪咖啡馆提供专业的管理解决方案，帮助店主高效管理猫咪、咖啡和顾客信息。</p>
            </div>

            <div class="footer-column">
                <h3>快速链接</h3>
                <ul class="footer-links">
                    <li><a href="index.html">首页</a></li>
                    <li><a href="cat.html">猫咪信息</a></li>
                    <li><a href="cafe.html">咖啡菜单</a></li>
                    <li><a href="">在线预订</a></li>
                    <li><a href="center.html">个人中心</a></li>
                </ul>
            </div>

            <div class="footer-column">
                <h3>联系方式</h3>
                <div class="contact-info">
                    <p>地址：广东科技学院</p>
                    <p>电话：666666</p>
                    <p>邮箱：666666.com</p>
                    <p>营业时间：10:00-18:00</p>
                </div>
            </div>
        </div>

        <div class="copyright">
            <p>© 2025 猫咖管理系统 版权所有 | 设计开发：会非的杨</p>
        </div>
    </div>
</footer>

<script>
    const API_BASE_URL = 'http://localhost:8080/catcafe';

    document.addEventListener('DOMContentLoaded', function () {
        // 用户信息初始化
        const username = localStorage.getItem('username');
        const role = localStorage.getItem('role');
        // 获取显示用户信息的元素
        const userDisplay = document.getElementById('user-display');

        // 如果有用户信息，显示在导航栏
        if (username) {
            // 根据角色显示不同的文本
            const roleText = role === 'admin' ? '管理员' : '普通用户';
            userDisplay.textContent = `${roleText}：${username}`;

            // 添加角色图标和样式（可选）
            userDisplay.innerHTML = `            <i class="fas fa-user-circle"></i>
            <span class="username-text">${username}</span>
            <span class="role-badge">${roleText}</span>
        `;
        } else {
            userDisplay.textContent = "未登录";
        }

        // 退出按钮功能
        const logoutBtn = document.getElementById('logout-btn');
        logoutBtn.addEventListener('click', function () {
            // 清除存储的用户信息
            localStorage.removeItem('username');
            localStorage.removeItem('role');

            // 提示用户并跳转到登录页
            alert('您已成功退出');
            window.location.href = 'login.html'; // 替换为你的登录页面路径
        });

        // 页面加载时获取订单
        loadOrders();

        // 加载订单数据
        async function loadOrders() {
            const ordersContainer = document.getElementById('ordersContainer');
            ordersContainer.innerHTML = `
                <div class="empty-state">
                    <i class="fas fa-spinner fa-spin"></i>
                    <h3>正在加载订单数据...</h3>
                </div>
            `;

            try {
                // 发送真实请求获取订单
                const response = await fetch(`${API_BASE_URL}/orders`);

                if (!response.ok) {
                    throw new Error(`加载订单失败: ${response.status} ${response.statusText}`);
                }

                const result = await response.json();

                // 根据后端返回的数据结构调整
                // 假设返回的数据在result.data中
                const orders = result.data ? result.data : result;

                if (!orders || orders.length === 0) {
                    showEmptyState();
                    return;
                }

                renderOrders(orders);
            } catch (error) {
                console.error('获取订单失败:', error);
                ordersContainer.innerHTML = `
                    <div class="empty-state">
                        <i class="fas fa-exclamation-triangle"></i>
                        <h3>加载订单失败</h3>
                        <p>${error.message || '请检查网络连接或稍后重试'}</p>
                    </div>
                `;
            }
        }

        // 渲染订单列表
        function renderOrders(orders) {
            const ordersContainer = document.getElementById('ordersContainer');
            ordersContainer.innerHTML = '';

            orders.forEach(order => {
                const orderCard = document.createElement('div');
                orderCard.className = 'order-card';

                // 提取咖啡名称（假设后端返回的字段是coffeeName）
                const coffeeName = order.coffeeName || '未知咖啡';

                orderCard.innerHTML = `
                    <div class="order-header">
                        <div class="order-info">
                            <div class="order-id">订单号: ${order.orderId}</div>
                            <div class="order-time">下单时间: ${formatTime(order.time)}</div>
                        </div>
                        <div class="total-price">¥${parseFloat(order.total || 0).toFixed(2)}</div>
                    </div>

                    <div class="coffee-name">
                        <i class="fas fa-coffee"></i> ${coffeeName}
                    </div>

                    <div class="order-details">
                        <div class="detail-item">
                            <span class="detail-label">用户ID</span>
                            <span class="detail-value">${order.userId || '未提供'}</span>
                        </div>

                        <div class="detail-item">
                            <span class="detail-label">数量</span>
                            <span class="detail-value">${order.number || 1}</span>
                        </div>

                        <div class="detail-item">
                            <span class="detail-label">口味要求</span>
                            <span class="detail-value">${order.flavor || '标准'}</span>
                        </div>
                    </div>

                    <div class="detail-item">
                        <span class="detail-label">备注</span>
                        <span class="detail-value">${order.note || '无备注'}</span>
                    </div>

                    <div class="order-actions">
                        <button class="delete-btn" data-order-id="${order.id}">
                            <i class="fas fa-trash-alt"></i> 删除订单
                        </button>
                    </div>
                `;

                ordersContainer.appendChild(orderCard);
            });

            // 绑定删除按钮事件
            document.querySelectorAll('.delete-btn').forEach(button => {
                button.addEventListener('click', function() {
                    const orderId = this.getAttribute('data-order-id');
                    deleteOrder(orderId);
                });
            });
        }

        // 删除订单
        async function deleteOrder(orderId) {
            if (!confirm(`确定要删除订单 ${orderId} 吗？此操作不可撤销。`)) {
                return;
            }

            try {
                // 发送真实DELETE请求
                const response = await fetch(`${API_BASE_URL}/orders/${orderId}`, {
                    method: 'DELETE'
                });

                if (!response.ok) {
                    throw new Error(`删除订单失败: ${response.status} ${response.statusText}`);
                }

                const result = await response.json();

                if (result.code===1) {
                    alert(`订单 ${orderId} 已成功删除！`);
                    loadOrders(); // 刷新订单列表
                } else {
                    throw new Error(result.message || '删除订单失败');
                }
            } catch (error) {
                console.error('删除订单出错:', error);
                alert(`删除订单失败: ${error.message || '请稍后再试'}`);
            }
        }

        // 空状态展示
        function showEmptyState() {
            const ordersContainer = document.getElementById('ordersContainer');
            ordersContainer.innerHTML = `
                <div class="empty-state">
                    <i class="fas fa-box-open"></i>
                    <h3>暂无订单信息</h3>
                    <p>当前没有可显示的订单记录</p>
                </div>
            `;
        }

        // 时间格式化函数
        function formatTime(isoString) {
            if (!isoString) return '未知时间';

            try {
                const date = new Date(isoString);
                return date.toLocaleString('zh-CN', {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit'
                });
            } catch (e) {
                return isoString; // 如果无法解析，返回原始字符串
            }
        }
    });
</script>

</body>
</html>